var Logic	= {
	board	: null,
	score	: 0,
	BLOCK	: false,

	actions : null,

	selection	: null,
	
	start	: function(){
		var w	= 240;
		var h	= 340;
		var nx	= 6;
		var ny	= 10;

		UI.start(w, h, nx, ny);

		this.score	= 0;
		this.board	= new Array();
		this.actions	= new Array();

		for(var x = 0; x < nx; x++){
			this.board.push(new Array());
		
			for(var y = 0; y < ny; y++){
				var r = this.getRandom(nx-1);
				if(r > 3){
					r = 0;
				}
				this.board[x].push(r+1);	
			}
		}
		this.updateBoard();
		this.updateScore();
		this.updateActions();
		window.setTimeout("Logic.superCheck()", 100);
	},

	onMouseDown	: function(tile){
		this.selectTile(tile);
		this.updateActions();
	},
	
	onMouseMove	: function(tile){
		if(this.switchTile(tile)){
			this.updateActions();
		}
		
	},

	onMouseUp	: function(){
		this.deselectTile();
		this.updateActions();
	},

	selectTile	: function(tile){
		var newTile = this.getTile(tile.x, tile.y);
		if(newTile.v != 0){
			this.selection = newTile;
			this.updateSelection();
		}
	},

	deselectTile	: function(){
		if(this.selection != null){
			var tile = this.getTile(this.selection.x, this.selection.y);
			this.updateDeselection();
			this.selection = null;
		}
	},
	
	removeTile		: function(pos){
		this.board[pos.x][pos.y] = 0;
	},
	
	getTile		: function(x, y){
		var tile = Tools.newTile(x, y, this.board[x][y]);
		return tile;
	},

	switchTile	: function(tile){
		if(this.selection != null){
			if(this.selection.x != tile.x){
				var oldTile = this.getTile(this.selection.x, this.selection.y);
				var newTile = this.getTile(tile.x, tile.y);

				this.board[oldTile.x][oldTile.y] = newTile.v;
				this.board[newTile.x][newTile.y] = oldTile.v;

				this.updateSwitch(oldTile, newTile);
		
				newTile.v = oldTile.v;
				this.selectTile(newTile);

				this.superCheck();
				return true;
			}
		}
		return false;
	},

	superCheck	: function(){
		this.fallTiles();
		this.checkAll();
		
		while(this.fallTiles()){
			this.checkAll();
		}
		this.updateActions();
	},

	fallTiles		: function(){
		var falling = new Array();
		var all = new Array();
		for(var x = 0; x < this.board.length; x++){
			var f = 0;
			for(var y = this.board[x].length; y >= 0; y--){
				if(this.board[x][y] == 0){
					f++;
				}else{
					if(f > 0){
						falling.push(Tools.newTile(x, y, this.board[x][y], f));
					}
					all.push(Tools.newTile(x, y, this.board[x][y], f));
				}
			}
		}
		for(f in falling){
			var x = falling[f].x;
			var y = falling[f].y;
			var t = falling[f].t;
			var v = falling[f].v;
			var dist = falling[f].f;
			this.board[x][y] = 0;
			this.board[x][y+dist] = v;
		}
		
		if(falling.length > 0){
			this.updateFalling(all);
			this.selection = null;
		}
		return falling.length > 0;
	},

	updateActions	: function(){
		UI.doStuff(this.actions);
	},

	checkAll	: function(){
		var done = false;
		for(x in this.board){
			for(y in this.board[x]){
				if(this.board[x][y] != 0){
					done |= this.checkTile(Tools.newCoord(x, y));
				}
			}
		}
	},

	checkTile		: function(pos){
		var tile = this.getTile(pos.x, pos.y);

		var x = y = v = 0;

		x = Math.abs(tile.x);
		y = Math.abs(tile.y);
		v = tile.v;
		
		var tilesV	= new Array();
		for(var yy = y; yy >= 0 && this.board[x][yy] == v; yy--){
			tilesV.push(this.getTile(x, yy));
		}
		for(var yy = y+1; yy < this.board[0].length && this.board[x][yy] == v; yy++){
			tilesV.push(this.getTile(x, yy));
		}

		var tilesH	= new Array();
		for(var xx = x; xx >= 0 && this.board[xx][y] == v; xx--){
			tilesH.push(this.getTile(xx, y));
		}
		for(var xx = x+1; xx < this.board.length && this.board[xx][y] == v; xx++){
			tilesH.push(this.getTile(xx, y));
		}

		
		var tilesHV = new Array();

		if(tilesH.length > 2){
			for(t in tilesH){
				tilesHV.push(tilesH[t]);
			}
		}
		if(tilesV.length > 2){
			for(t in tilesV){
				tilesHV.push(tilesV[t]);
			}
		}
		
		if(tilesHV.length > 0){
			this.removeTiles(tilesHV);
			this.addScore(tilesHV.length);
		}
		
		return tilesHV.length > 0;
	},

	removeTiles		: function(tiles){
		for(t in tiles){
			this.removeTile(tiles[t]);
		}
		this.updateRemove(tiles);
	},

	getRandom		: function(n){
		return Math.floor(Math.random()*n);
	},

	addScore	: function(n){
		if(n < 3){
			return false;
		}
		var add		= 7;
		var bonus	= Math.pow(3, n-3);
		var points	= add * bonus;
		var result	= this.score + points;
		this.score = result;
		this.updateScore();
	},
	
	updateBoard	: function(){
		this.actions.push(Tools.actionBoard(this.board));
	},

	updateScore	: function(){
		this.actions.push(Tools.actionScore(this.score));
	},
	
	updateSelection	: function(){
		this.actions.push(Tools.actionSelection(this.selection));
	},
	
	updateDeselection	: function(){
		this.actions.push(Tools.actionDeselection(this.selection));
	},
	
	updateSwitch	: function(tileA, tileB){
		var tiles = new Array(2);
		tiles[0] = Tools.newTile(tileA.x, tileA.y, tileA.v);
		tiles[1] = Tools.newTile(tileB.x, tileB.y, tileB.v);
		this.actions.push(Tools.actionSwitch(tiles));
	},

	updateRemove	: function(tiles){
		this.actions.push(Tools.actionRemove(tiles));
	},
	
	updateFalling	: function(falling){
		this.actions.push(Tools.actionFalling(falling));
	}
}
